
//格式化部门
function formatterid(value,row,index) {
    if(value){return value.id;}
}

function formatterparentId(value,row,index) {


    if(value){return value.name;}
}


//格式化状态
function formatterState(value,row,index) {
    if(value == 0){ return "正常";}
    return "<span style='color: red'>报废</span>";
}


$(function () {
    //变量抽取
    var dept_datagrid = $("#dept_datagrid");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");

    dept_datagrid.datagrid({
        url:'/dept/list',
        fitColumns:true,//
        toolbar:"#toolbar",//
        striped:true,//
        rownumbers:true,//
        singleSelect:true,//
        pagination:true,//
        pagePosition:"bottom",//
        fit:true,//
        sortName:"hireDate",
        sortOrder:"desc",//
        columns:[[
            {field:'id',hidden:true},
            {field:"sn",title:'编号',width:100},
            {field:'name',title:'名称',width:100},
            {field:'manager',title:'经理',width:100,formatter:formatterid},
            // {field:'parent',title:'上级部门',width:100,formatter:formatterparentId},
            {field:'state',title:'状态',width:100,formatter:formatterState},

        ]],
        onClickRow:function (index, row) {
            if(row.state) {
                $("#changeStatus_btn").linkbutton({text:"离职"})
            } else {
                $("#changeStatus_btn").linkbutton({text:"复职"})
            }
        }
    });

    dept_dialog.dialog({
        title:"部门编辑",
        width:350,
        height:250,
        modal: true,
        buttons:"#form_btns",
        closed: true,
        onClose:function () {
            $("#dept_form").form("clear");
        }
    })

    var methodObj = {
        // 新增
        add:function add() {
            dept_dialog.dialog("setTitle",'新增部门');
            dept_dialog.dialog("open");
        },
        //保存
        save:function save() {
            dept_form.form('submit', {
                url:'/dept/save',
                success: function(data){

                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提示",'保存成功','info',function () {
                            dept_datagrid.datagrid('reload');
                            methodObj.cancel();
                        })
                    } else {
                        $.messager.alert("温馨提示",data.msg,'error');
                    }
                }
            });


        },
        //编辑
        edit:function edit() {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示',"请选中一条数据",'warning');
                return;
            }
            dept_dialog.dialog("setTitle",'编辑部门');
            dept_dialog.dialog("open",true);
            dept_form.form('load',row);
        },
        //改变状态
        changeState:function changeState() {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示',"请选中一条数据",'warning');
                return;
            }
            $.messager.confirm("确认对话框","您确定要报废这个部门吗?",function (r) {
                if (r) {
                    $.get("/dept/delete",{id:row.id},function (data) {
                        console.log(data);
                        if(data.success) {
                            $.messager.alert("温馨提示",data.msg,'info',function () {
                                dept_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示",data.msg,'error');
                        }
                    });

                }
            })

        },
        //取消
        cancel:function cancel() {
            dept_dialog.dialog("close",true);
        },
        //刷新
        reload:function reload() {
            dept_datagrid.datagrid("load");
        },
        //高级查询
        query:function query() {
            var keyword = $("[name=keyword]").val();

            dept_datagrid.datagrid('load',{
                keyword:keyword,
            });
        }
    };

    //调用方法
    $('a[data-cmd]').click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

})

